package com.hzit.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.xml.stream.events.EndDocument;
import java.io.IOException;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebFilter(urlPatterns = "/*",initParams = {
		@WebInitParam(name = "encoding",value = "utf8")     // 设置过滤器时，可以为过滤器设置一个参数：参数名为encoding，值：utf8
})
public class MyFilter implements Filter {
	private String encoding;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		encoding = filterConfig.getInitParameter("encoding");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
		response.setContentType("application/json;charset=" + encoding);
		request.setCharacterEncoding(encoding);     //解决post提交的中文乱码问题
		filterChain.doFilter(request,response);
	}

	@Override
	public void destroy() {

	}
}
